package com.gxa.service.impl;

import com.gxa.mapper.OutStorageMapper;
import com.gxa.pojo.GoodsStorage;
import com.gxa.pojo.OutStorage;
import com.gxa.service.OutStorageService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.sql.Timestamp;
import java.util.Calendar;
import java.util.List;
import java.util.Map;

/**
 * @author 陈静
 * @date 2020/9/25 14:41
 */
@Service(value = "outStorageService")
public class OutStorageServiceImpl implements OutStorageService {
    @Resource
    OutStorageMapper outStorageMapper;
    /**
     * 查询所有仓库名字
     * @return 供仓库名字
     */
    @Override
    @Transactional(propagation = Propagation.SUPPORTS,isolation = Isolation.DEFAULT,rollbackFor = Throwable.class)
    public List<Map<String,Object>> findStoreName() {
        return outStorageMapper.findStoreName() ;
    }

    /**
     * 查询指定日期范围，出库仓库，客户名称，审核状态 的出库信息
     * @param map
     * @return
     */
    @Override
    @Transactional(propagation = Propagation.SUPPORTS,isolation = Isolation.DEFAULT,rollbackFor = Throwable.class)
    public List<OutStorage> findOutStorage(Map map) {
        return outStorageMapper.findOutStorage(map);
    }

    @Override
    @Transactional(propagation = Propagation.SUPPORTS,isolation = Isolation.DEFAULT,rollbackFor = Throwable.class)
    public Integer countFindOutStorage(Map<String, Object> map) {
        return outStorageMapper.countFindOutStorage(map);
    }

    /**
     *  分页查询
     * @param page 起始页
     * @param limit 分页大小
     * @return
     */
    @Override
    @Transactional(propagation = Propagation.SUPPORTS,isolation = Isolation.DEFAULT,rollbackFor = Throwable.class)
    public List<OutStorage> findAllOutStorageByPage(Integer page, Integer limit) {
        return outStorageMapper.findOutStorageByPage(page,limit);
    }

    /**
     *  查询所有的数据数目
     * @return
     */
    @Override
    @Transactional(propagation = Propagation.SUPPORTS,isolation = Isolation.DEFAULT,rollbackFor = Throwable.class)
    public Integer findAllOutStorage() {
        return outStorageMapper.findAllOutStorage();
    }

    /**
     *  修改审批状态
     * @param map
     * @return 返回影响行数
     */
    @Override
    @Transactional(propagation = Propagation.REQUIRED,isolation = Isolation.DEFAULT,rollbackFor = Throwable.class)
    public Integer updateState(Map map) {
        Timestamp timestamp = new Timestamp(Calendar.getInstance().getTimeInMillis());
        map.put("outStorageTime",timestamp);
        Integer integer = outStorageMapper.updateState(map);
        Integer id = (Integer) map.get("id");
        List<Map> outStorageGoods = outStorageMapper.findOutStorageGoodss(id);
        for (Map outStorageGood : outStorageGoods) {
            String goods = (String) outStorageGood.get("goods");
            GoodsStorage goodsStorageByName = outStorageMapper.findGoodsStorageByName(goods);
            if(goodsStorageByName!=null){
                outStorageMapper.updateGoodsStorageStock((Double) outStorageGood.get("bQuantity"),(Double) outStorageGood.get("sQuantity"),goodsStorageByName.getId());
            }
        }
        return integer;
    }

    /**
     * 查询所有的客户名称
     * @return
     */
    @Override
    @Transactional(propagation = Propagation.SUPPORTS,isolation = Isolation.DEFAULT,rollbackFor = Throwable.class)
    public List<Map<String,Object>> findCustomerName() {
        return outStorageMapper.findCustomerName();
    }

    /**
     * 查询所有品牌的名称
     * @return
     */
    @Override
    @Transactional(propagation = Propagation.SUPPORTS,isolation = Isolation.DEFAULT,rollbackFor = Throwable.class)
    public List<Map<String,Object>> findBrandName() {
        return outStorageMapper.findBrandName();
    }

    /**
     * 查询所有品牌分类名称
     * @return
     */
    @Override
    @Transactional(propagation = Propagation.SUPPORTS,isolation = Isolation.DEFAULT,rollbackFor = Throwable.class)
    public List<Map<String,Object>> findLevelName() {
        return outStorageMapper.findLevelName();
    }
    /**
     * 根据品牌、分类查询商品信息
     * @param map 品牌 分类
     * @return
     */
    @Override
    @Transactional(propagation = Propagation.SUPPORTS,isolation = Isolation.DEFAULT,rollbackFor = Throwable.class)
    public List<Map<String, Object>> findOutStorageGoods(Map<String, Object> map) {
        return outStorageMapper.findOutStorageGoods(map);
    }

    @Override
    @Transactional(propagation = Propagation.SUPPORTS,isolation = Isolation.DEFAULT,rollbackFor = Throwable.class)
    public Integer countFindOutStorageGoods(Map<String, Object> map) {
        return outStorageMapper.countFindOutStorageGoods(map);
    }

    /**
     *  通过id查询入库信息
     * @param id
     * @return
     */
    @Transactional(propagation = Propagation.SUPPORTS,isolation = Isolation.DEFAULT,rollbackFor = Throwable.class)
    @Override
    public OutStorage findOutStorageById(Integer id) {
        return outStorageMapper.findOutStorageById(id);
    }

    /**
     *  通过入库id查询对应的商品信息
     * @param outStorageId
     * @return
     */
    @Transactional(propagation = Propagation.SUPPORTS,isolation = Isolation.DEFAULT,rollbackFor = Throwable.class)
    @Override
    public List<Map<String, Object>> findOutStorageGoodsByOutStorageId(Integer outStorageId) {
        return outStorageMapper.findOutStorageGoodsByOutStorageId(outStorageId);
    }

    /**
     *  通过id删除出库信息
     * @param id
     * @return
     */
    @Transactional(propagation = Propagation.REQUIRED,isolation = Isolation.DEFAULT,rollbackFor = Throwable.class)
    @Override
    public Integer deleteOutStorageById(Integer id) {
        return outStorageMapper.deleteOutStorageById(id);
    }

    /**
     *  通过出库id删除对应的出库商品信息
     * @param id
     * @return
     */
    @Transactional(propagation = Propagation.REQUIRED,isolation = Isolation.DEFAULT,rollbackFor = Throwable.class)
    @Override
    public Integer deleteOutStorageGoodsByOutStorageId(Integer id) {
        return outStorageMapper.deleteOutStorageGoodsByOutStorageId(id);
    }

    /**
     * 全查询
     * @return
     */
    @Override
    @Transactional(propagation = Propagation.REQUIRED,isolation = Isolation.DEFAULT,rollbackFor = Throwable.class)
    public List<Map<String, Object>> findAllOutStorages() {
        return outStorageMapper.findAllOutStorages();
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED,isolation = Isolation.DEFAULT,rollbackFor = Throwable.class)
    public Integer countFindAllOutStorages() {
        return outStorageMapper.countFindAllOutStorages();
    }


    /**
     * 插入出库信息
     * @param
     * @return
     */
    @Override
    @Transactional(propagation = Propagation.REQUIRED,isolation = Isolation.DEFAULT,rollbackFor = Throwable.class)
    public Integer insertOutStorage(Map map) {
        Map outstorage = (Map) map.get("outstorage");
        Timestamp timestamp = new Timestamp(Calendar.getInstance().getTimeInMillis());
        outstorage.put("createdDate",timestamp);
        Integer integer = outStorageMapper.insertOutStorage(outstorage);
        Integer outStorageId = outStorageMapper.findOutStorageId();
        List<Map> goods = (List<Map>) map.get("outstoragegoods");
        for(Map good:goods) {
            good.put("outStorageId",outStorageId);
            outStorageMapper.insertOutStorageGoodss(good);
//            String name = (String) good.get("goods");
//            GoodsStorage goodsStorageByName = outStorageMapper.findGoodsStorageByName(name);
//            if (goodsStorageByName!=null){
//                outStorageMapper.updateGoodsStorageStock((Integer) good.get("bQuantity"),(Integer) good.get("sQuantity"),goodsStorageByName.getId());
//            }
        }
        return integer;
    }

    /**
     *  更新出库信息
     * @param map
     * @return
     */
    @Override
    @Transactional(propagation = Propagation.SUPPORTS,isolation = Isolation.DEFAULT,rollbackFor = Throwable.class)
    public Integer updateOutStorage(Map map) {
        Map outstorage = (Map) map.get("outstorage");
        int id = Integer.parseInt(outstorage.get("id").toString());
        outstorage.put("id",id);
        Integer integer = outStorageMapper.updateOutStorage(outstorage);
        List<Map> outStorageGoodss = outStorageMapper.findOutStorageGoodss(id);
        for (Map storageGoodss : outStorageGoodss) {
            String goods = (String) storageGoodss.get("goods");
            GoodsStorage goodsStorageByName = outStorageMapper.findGoodsStorageByName(goods);
            if(goodsStorageByName!=null){
                outStorageMapper.updateGoodsStorageStock2((Double) storageGoodss.get("bQuantity"),(Double) storageGoodss.get("sQuantity"),goodsStorageByName.getId());
            }
        }
        Integer integer1 = outStorageMapper.deleteOutStorageGoods(outstorage);

        List<Map> goods = (List<Map>) map.get("outstoragegoods");
        for (Map good : goods) {
            int outstorageId = Integer.parseInt(good.get("outstorageId").toString());
            good.put("outstorageId",outstorageId);
            outStorageMapper.insertOutStorageGoodsByOutStorageId(good);
            String goods1 = (String) good.get("goods");
            GoodsStorage goodsStorageByName = outStorageMapper.findGoodsStorageByName(goods1);
            if(goodsStorageByName!=null){
                outStorageMapper.updateGoodsStorageStock3((Integer) good.get("bQuantity"),( Integer) good.get("sQuantity"),goodsStorageByName.getId());
            }else{
                outStorageMapper.insertGoodsStorage(good);
            }
        }
        return integer;
    }
}
